# -*- coding: utf-8 -*-

import os
import warnings
import uuid
import pandas as pd
import pandas_profiling


class CommonFunction:
    def __init__(self, data=None, columns=None):
        if not(isinstance(data, pd.DataFrame)):
            msg = 'data should be an instance of pandas.DataFrame.'
            warnings.warn(msg)

        if columns is None:
            self._data = data
        else:
            self._data = data.loc[:, columns]

        self._data.index = [i for i in range(self._data.shape[0])]

    def set_data(self, data=None, columns=None):
        if not (isinstance(data, pd.DataFrame)):
            msg = 'data should be an instance of pandas.DataFrame.'
            warnings.warn(msg)

        if columns is None:
            self._data = data
        else:
            self._data = data.loc[:, columns]

        self._data.index = [i for i in range(self._data.shape[0])]

    def get_data(self, columns=None):
        if columns is None:
            return self._data
        else:
            return self._data.loc[:, columns]

    def profile_report(self, filename=None):
        if filename is None:
            uuid_str = str(uuid.uuid4())
            filename = 'tmpfile_%s.html' % uuid_str
        if filename[-5:] != '.html':
            filename += '.html'

        filename = 'html/' + filename

        profile = self._data.profile_report()
        profile.to_file(output_file=filename)

        return os.path.abspath(filename)
